<template>
  <div class="common-layout">
    <el-container>
      <el-header>
        <div class="div-hello">
          <span>你好，{{ store.state.admin.admin_name }}</span>
          <el-button type="danger" size="small" @click="handleLogout">退出</el-button>
        </div>
      </el-header>
      <el-container>
        <el-aside>
          <el-menu router default-active="/home" class="el-menu-vertical-demo">
            <el-sub-menu v-for="item in store.state.admin.permissionList" :key="item.model_id" :index="item.model_id">
              <template #title>
                <el-icon>
                  <setting v-if="item.model_id == 1" />
                  <icon-menu v-else />
                </el-icon>
                <span>{{ item.model_name }}</span>
              </template>
              <!-- v-for和v-if不能同层使用，所以外面需要套一个template标签 -->
              <template v-for="item2 in item.permissionList">
                <el-menu-item v-if="item2.type == 2" :index="item2.path" :key="item2.permission_id">
                  {{ item2.permission_name }}
                </el-menu-item>
              </template>
            </el-sub-menu>
          </el-menu>
        </el-aside>
        <el-main>
          <!-- 路由出口 -->
          <router-view #default="{ Component }">
            <keep-alive>
              <component :is="Component" />
            </keep-alive>
          </router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
<script lang="ts" setup>
import { useStore } from 'vuex';
import { useRouter } from 'vue-router';

import {
  Menu as IconMenu,
  Setting,
} from '@element-plus/icons-vue';

// 钩子函数：使用vuex
const store = useStore();
const router = useRouter();

const handleLogout = () => {
  // 清空vuex中的数据
  store.commit("setAdmin", {});
  // 跳转到登录页
  router.push('/');
};
</script>
<style lang="scss" scoped>
.el-header {
  display: flex;
  flex-direction: row-reverse;
  height: 60px;

  .div-hello {
    display: flex;
    align-items: center;

    span {
      padding-right: 10px;
    }
  }
}

.el-container {

  border-top: 1px solid var(--el-border-color);

  .el-aside {
    width: 200px;

    .el-menu {
      height: calc(100vh - 60px);
    }
  }

}
</style>